package com.ruoyi.project.applet.service.impl;


import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.applet.domain.Record;
import com.ruoyi.project.applet.mapper.RecordMapper;
import com.ruoyi.project.applet.service.InteractService;
import com.ruoyi.project.applet.service.RecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {
    @Resource
    private RecordMapper recordDao;
    @Resource
    private InteractService interactService;

    @Override
    public List<Record> selectByIssend(Integer u_id) {
        return recordDao.selectByIssend(Long.valueOf(u_id));
    }

    @Override
    public Integer updateById(Integer r_id) {
        return recordDao.updateById(Long.valueOf(r_id));
    }

    @Override
    public long isrecord(Integer from_id, Integer to_id) {
        return recordDao.isrecord(from_id, to_id);
    }

    @Override
    public Integer add(Integer from_id, Integer to_id, String r_content, Integer r_type, String chatid, String r_createTime) {
        return recordDao.add(from_id, to_id, r_content, r_type, chatid, r_createTime);
    }

    @Override
    public Integer updateread(Integer from_id, Integer to_id) {
        return recordDao.updateread(from_id, to_id);
    }

    @Override
    public AjaxResult selectBychatid(Long accountId, Integer from_id, Integer id) {
        updateread(from_id, accountId.intValue());
        interactService.updatecount(from_id, accountId.intValue());
        String chatid = "";
        List<Record> records = recordDao.selectByfromto(Math.toIntExact(accountId), from_id);
        if (null == records || records.size() == 0) {
            List<Record> records1 = recordDao.selectByfromto(from_id, Math.toIntExact(accountId));
            Record record = records1.stream().findFirst().orElse(null);
            if (EmptyUtils.isEmpty(record)) {
                return AjaxResult.success();
            }
            chatid = record.getChatId();
        } else {
            Record record = records.stream().findFirst().orElse(null);
            chatid = record.getChatId();
        }
        if (id == 0) {
            //第一次获取
            List<Record> depts = recordDao.selectBychatid1(chatid);
            return AjaxResult.success(depts);
        } else {
            //不是第一次获取
            List<Record> depts = recordDao.selectBychatid1(chatid);
//                List<Record> depts = recordDao.selectBychatid(chatid, id);
            return AjaxResult.success(depts);
        }

    }


}
